// pages/loginAndRegister/loginAndRegister.js
// 倒计时函数
const app = getApp()
var interval = null
var phoneNumValue = ''
var authNumValue = ''
var requestAuthNumValue = ''
var jumpType = 1
var loginTitle = wx.getStorageSync('latelyUserNickname')
var userIcon = wx.getStorageSync('latelyUserIcon')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arrowImage: '/images/downArrow.png',
    time: '获取验证码',
    currentTime: 60,
    phoneNumCount: 0,
    isCanCommite: false,
    // 是否弹出手机号键盘
    isPhonePopupKB: false,
    // 是否弹出验证码键盘
    isAuthPopupKB: false,
    // 从哪个页面进来的
  },
  // 监听验证码的输入
  authCodeInput: function(e) {
    console.log(e.detail.value)
    authNumValue = e.detail.value
    var that = this
    if (authNumValue.length == 4) {
      that.setData({
        authNumValue: true
      })
    } else {
      that.setData({
        authNumValue: false
      })
    }
  },
  // 监听手机号的输入
  phoneInput: function(e) {
    console.log(e.detail.value)
    phoneNumValue = e.detail.value
    if (phoneNumValue == 11) {
      this.setData({
        disabled: false
      })
    }
    var that = this
    that.setData({
      phoneNumCount: phoneNumValue.length
    })
  },
  // 验证码倒计时
  getCode: function(options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function() {
      currentTime--;
      that.setData({
        time: '倒计时: ' + currentTime + 's'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新获取验证码',
          currentTime: 60,
          disabled: false
        })
      }
    }, 1000)
  },
  // 点击获取验证码进入倒计时
  getVerificationCode: function() {
    var that = this
    var phonetel = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (phonetel.test(phoneNumValue)) {
      that.getCode();
      that.loadAuthCodeData();
      that.setData({
        disabled: true,
        time: '倒计时: ' + 60 + 's',
      })
    } else {
      wx.showToast({
        title: '手机号有误',
        icon: 'success',
        duration: 2000
      })
    }
  },
  // 点击提交按钮
  commiteBtnClick: function() {
    var that = this;
    if (requestAuthNumValue == authNumValue) {
      // 输入的验证码正确
      that.loadFastLoginData()
    } else {
      wx.showToast({
        title: '验证码有误',
        icon: 'success',
        duration: 2000
      })
    }
  },
  // 验证码接口请求
  loadAuthCodeData() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.baseNet + 'user/sendOutCode?',
      data: {
        username: phoneNumValue,
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        console.log(res.data.sendcode)
        requestAuthNumValue = res.data.sendcode
        that.setData({
          isAuthPopupKB: true
        })
        wx.hideLoading()
        wx.showToast({
          title: '验证码已发送',
          duration: 1000,
        })
      },
    })
  },
  // 请求提交的接口
  loadFastLoginData() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.baseNet + 'user/fastLogin?',
      data: {
        username: phoneNumValue,
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        wx.hideLoading()
        // 保存手机号
        wx.setStorageSync('phoneNumber', phoneNumValue)
        wx.setStorageSync('laterlyPhoneNumber', phoneNumValue)
        wx.showToast({
          title: '登录成功',
          duration: 1000
        })
        // 给个人中心页面赋值
        var pages = getCurrentPages()
        var that = this
        var prevPage = pages[pages.length - 2]
        //判断是否授权获取用户信息
        wx.getSetting({
          success(res) {
            if (res.authSetting['scope.userInfo']) {
              // 保存昵称
              wx.setStorageSync('userNickname', loginTitle)
              // 保存头像
              wx.setStorageSync('userIcon', userIcon)
              // 授权登录手机号
              wx.setStorageSync('loginState', 'auth')
              var icon = wx.getStorageSync('userIcon')
              var title = wx.getStorageSync('userNickname')
              if (jumpType == 1) {
                //上一个页面
                prevPage = pages[pages.length - 2]
                setTimeout(function() {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 1000)
              } else {
                prevPage = pages[pages.length - 3]
                setTimeout(function() {
                  wx.navigateBack({
                    delta: 2
                  })
                }, 1000)
              }
              // 用户已经授权
              prevPage.setData({
                loginTitle: title,
                userIcon: icon,
                disabled: true,
                backHidden: false,
                loginDisabled: true,
                phoneNum: phoneNumValue
              })
            } else {
              wx.setStorageSync('loginState', 'noAuth')
              wx.setStorageSync('noAuthLogin', 'noAuthLogin')
              prevPage.setData({
                disabled: true,
                backHidden: false,
                loginDisabled: true,
                phoneNum: phoneNumValue
              })
              prevPage.loadData(phoneNumValue)
              setTimeout(function() {
                wx.navigateBack({
                  delta: 1
                })
              }, 1000)
            }
          },
        })
        // var prevPage = pages[pages.length - 1]  //当前界面
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      jumpType = options.jumpType
      this.setData({
        isPhonePopupKB: true
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})